#!/bin/bash

JAVA_HOME=/usr/local/java

if [ -d "/usr/local/jdk1.8.0_77" ];then
	JAVA_HOME="/usr/local/jdk1.8.0_77"
fi

if [ "$1" = "kybidder" ]; then
 MAIN=com.kyhd.bidder.server.BidderApp
 SERVICE_NAME=kybidderapp
 PORT=9051
else
    echo "Sorry, $1 not recognized. Enter toutiao/report task!"
    exit 1
fi

cd `dirname $0`
BIN_DIR=`pwd`
cd ..
DEPLOY_DIR=`pwd`

LIB_DIR=$DEPLOY_DIR/lib
LIB_JARS=`ls $LIB_DIR|grep .jar|awk '{print "'$LIB_DIR'/"$0}'|tr "\n" ":"`

LOGS_DIR=/data/log/${SERVICE_NAME}
mkdir -p $LOGS_DIR

echo -e "Starting the ${SERVICE_NAME} server ...\c"

STDERRLOG=$LOGS_DIR/stderr.log

JAVA_OPTS=" -server -d64 -Dport=${PORT} -Dapp.name=${SERVICE_NAME} -Dlogs.path=$LOGS_DIR "
if [ "@env@" = "test"  ];then
	JAVA_OPTS=" $JAVA_OPTS -Xmx128m -Xms128m -Xmn64m -XX:PermSize=32m -XX:MaxPermSize=32m -Xss256k "
else
	JAVA_OPTS=" $JAVA_OPTS -Xmx256m -Xms256m -Xmn128m -XX:PermSize=64m -XX:MaxPermSize=64m -Xss256k "
fi

JAVA_OPTS=" $JAVA_OPTS -Xloggc:$LOGS_DIR/${SERVICE_NAME}_gc.log -XX:HeapDumpPath=$LOGS_DIR -XX:+DisableExplicitGC "
JAVA_OPTS=" $JAVA_OPTS -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=70 -XX:+PrintGCDateStamps -XX:+HeapDumpOnOutOfMemoryError "

BUILD_ID=dontKillMe nohup $JAVA_HOME/bin/java $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_JMX_OPTS -classpath $LIB_JARS $MAIN > $STDERRLOG 2>&1 &
